package com.cbank.controller.pc;

import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cbank.domain.*;
import com.cbank.service.AboutUsBannerService;
import com.cbank.service.AboutUsService;
import com.cbank.utils.JSONUtil;
import com.cbank.utils.NumConvertEnEnum;
import com.cbank.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cbank.service.ModuleShowDetailService;
import com.cbank.service.ModuleShowService;
import com.cbank.vo.ModuleVo;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/pc")
@ResponseBody
public class HomeController {
    @Autowired
    private ModuleShowService moduleShowService;
    @Autowired
    private ModuleShowDetailService moduleShowDetailService;
    @Autowired
    private AboutUsService aboutUsService;
    @Autowired
    private AboutUsBannerService aboutUsBannerService;
    Logger logger = Logger.getLogger(HomeController.class);

    @RequestMapping(value = "/educationDetail", method = RequestMethod.GET)
    public String educationDetail(@RequestParam(value = "moduleShowId") Integer moduleShowId, Model model) {
        logger.info("---------welcome--------educationDetail");
        ModuleShow moduleShow = moduleShowService.getById(moduleShowId);
        JSONArray jsonArray = JSONUtil.parseArray(moduleShow.getBannerUrl());
        List<String> bannerUrlList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject myjObject = jsonArray.getJSONObject(i);
            logger.info(myjObject.getString("url"));
            bannerUrlList.add(myjObject.getString("url"));
        }
        List<ModuleShowDetail> wzList = moduleShowDetailService.getListByModuleShowIdAndType(moduleShowId, ModuleShowDetail.CONTENTTYPE_WZ);
        StringUtil.subActrive(wzList);
        List<ModuleShowDetail> ljList = moduleShowDetailService.getListByModuleShowIdAndType(moduleShowId, ModuleShowDetail.CONTENTTYPE_LJ);
        if(wzList.size()!=0){
            model.addAttribute("moduleShowDetailList", wzList);
            model.addAttribute("bannerUrlList", bannerUrlList);
            model.addAttribute("moduleShow", moduleShow);
            logger.info("---------end--------educationDetail");
            return "/pc/educationDetail";
        }else{

            Map<String, Object> moduleShowDetailMap = new HashMap<>();
            moduleShowDetailMap.put("bannerUrlList", bannerUrlList);
            moduleShowDetailMap.put("moduleShowTitle", moduleShow.getTitle());
            moduleShowDetailMap.put("moduleShowEnTitle", moduleShow.getEnTitle());
            moduleShowDetailMap.put("moduleShow", moduleShow);
            moduleShowDetailMap.put("wzList","");
            moduleShowDetailMap.put("ljList",ljList);

            model.addAttribute("moduleShowDetailMap", moduleShowDetailMap);

            return "/pc/showed";
        }

    }

    @RequestMapping(value = "/education", method = RequestMethod.GET)
    public String education(Model model) {
        logger.info("---------welcome--------education");
        List<ModuleShow> imageList = this.moduleShowService.getListByModuleIdAndType(2, ModuleShow.TYPE_GG);
        Collections.sort(imageList, new Comparator<ModuleShow>() {
            @Override
            public int compare(ModuleShow o1, ModuleShow o2) {
                return o2.getId()-o1.getId();
            }
        });
        List<ModuleShow> moduleShowList = this.moduleShowService.getListByModuleIdAndType(2, ModuleShow.TYPE_CON);
        ModuleVo moduleVo = new ModuleVo();
        moduleVo.setModuleShowList(moduleShowList);
        moduleVo.setImageList(imageList);
        model.addAttribute("moduleVo", moduleVo);
        return "/pc/education";
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        logger.info("---------welcome--------index");
        return "/pc/show";
    }

    @RequestMapping(value = "/news", method = RequestMethod.GET)
    public String news(Model model) {
        logger.info("---------welcome--------news");
        Map<String, Object> news = new TreeMap<>();

//		List<ModuleShow> imageList=this.moduleShowService.getListByModuleIdAndType(5,ModuleShow.TYPE_GG);
//		List<ModuleShow> moduleShowList=this.moduleShowService.getListByModuleIdAndType(5,ModuleShow.TYPE_CON);
//		logger.info("moduleShowList");
//		ModuleVo moduleVo=new ModuleVo();
//		moduleVo.setModuleShowList(moduleShowList);
//		moduleVo.setImageList(imageList);
//		model.addAttribute("moduleVo",moduleVo);
        List<String> date = moduleShowService.getAllDate(5);
//        if(date.size()!=0){
//            Collections.sort(date);
//        }
        for (String s : date) {
            logger.info("===============================:     "+s);
            List<ModuleShow> moduleShows = moduleShowService.getAllByDate(s, 5);
            for (ModuleShow m : moduleShows) {
                logger.info(m.getViewId());
            }
            news.put(s.substring(0, 4) + " " + NumConvertEnEnum.getByValue(Integer.valueOf(s.substring(s.lastIndexOf("-") + 1))).getDisplay(), moduleShows);

            logger.info(NumConvertEnEnum.getByValue(Integer.valueOf(s.substring(s.lastIndexOf("-") + 1))).getDisplay());
        }
        news=((TreeMap<String, Object>) news).descendingMap();
//		Iterator<Map.Entry<String, Object>> iterator = news.entrySet().iterator();
//		while (iterator.hasNext()){
//			Map.Entry<String,Object> entry=iterator.next();
//			logger.info(entry.getKey()+"      "+entry.getValue());
//		}
        model.addAttribute("news", news);
        return "/pc/news";
    }

    @RequestMapping(value = "/opendetail", method = RequestMethod.GET)
    public String openDetail() {
        logger.info("---------welcome--------openDetail");
        return "/pc/openDetail";
    }

    //展览
    @RequestMapping(value = "/show", method = RequestMethod.GET)
    public String show(Model model) {
        logger.info("---------welcome--------show1111111");
        List<ModuleShow> imageList = this.moduleShowService.getListByModuleIdAndType(1, ModuleShow.TYPE_GG);
        Collections.sort(imageList, new Comparator<ModuleShow>() {
            @Override
            public int compare(ModuleShow o1, ModuleShow o2) {
                return o2.getId()-o1.getId();
            }
        });
        List<ModuleShow> moduleShowList = this.moduleShowService.getListByModuleIdAndType(1, ModuleShow.TYPE_CON);
        ModuleVo moduleVo = new ModuleVo();
        moduleVo.setModuleShowList(moduleShowList);
        moduleVo.setImageList(imageList);
        model.addAttribute("moduleVo", moduleVo);
        return "/pc/show";
    }

    //展览预告
    @RequestMapping(value = "/showon", method = RequestMethod.GET)
    public String showOn(@RequestParam(value = "moduleShowId") Integer moduleShowId, Model model) {
        logger.info("---------welcome--------show" + moduleShowId);
        return "/pc/showOn";
    }

    @RequestMapping(value = "/showdaohewan", method = RequestMethod.GET)
    public String showdaohewan() {
        logger.info("---------welcome--------show");
        return "/pc/showDaoHeWan";
    }

    @RequestMapping(value = "/showed", method = RequestMethod.GET)
    public String showed(@RequestParam(value = "moduleShowId") Integer moduleShowId, Model model) {
        logger.info("---------welcome--------show     hhhhhhh");
        ModuleShow moduleShow = moduleShowService.getById(moduleShowId);
        List<ModuleShowDetail> wzList = moduleShowDetailService.getListByModuleShowIdAndType(moduleShowId, ModuleShowDetail.CONTENTTYPE_WZ);
        List<ModuleShowDetail> ljList = moduleShowDetailService.getListByModuleShowIdAndType(moduleShowId, ModuleShowDetail.CONTENTTYPE_LJ);
        Map<String, Object> moduleShowDetailMap = new HashMap<>();
        moduleShowDetailMap.put("wzList", wzList);
        moduleShowDetailMap.put("ljList", ljList);


        if (wzList.size() != 0) {
            if (wzList.size() == 1) {
                logger.info("文章为1");
                String bannerUrl = wzList.get(0).getBannerUrl();
                JSONArray jsonArray = JSONUtil.parseArray(bannerUrl);
                List<String> bannerUrlList = new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject myjObject = jsonArray.getJSONObject(i);
                    logger.info(myjObject.getString("url"));
                    bannerUrlList.add(myjObject.getString("url"));
                }
                moduleShowDetailMap.put("bannerUrlList", bannerUrlList);
                moduleShowDetailMap.put("moduleShow",moduleShow);
                moduleShowDetailMap.put("moduleShowDetail",wzList.get(0));

                model.addAttribute("map", moduleShowDetailMap);
                return "/pc/showDetailHuaQingSe";

            }

            logger.info("文章为多");
            JSONArray jsonArray = JSONUtil.parseArray(moduleShow.getBannerUrl());
            List<String> bannerUrlList = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject myjObject = jsonArray.getJSONObject(i);
                logger.info(myjObject.getString("url"));
                bannerUrlList.add(myjObject.getString("url"));
            }
            moduleShowDetailMap.put("bannerUrlList", bannerUrlList);
            moduleShowDetailMap.put("moduleShowTitle", moduleShow.getTitle());
            moduleShowDetailMap.put("moduleShowEnTitle", moduleShow.getEnTitle());
            moduleShowDetailMap.put("moduleShow", moduleShow);
            moduleShowDetailMap.put("wzList",wzList);
            moduleShowDetailMap.put("ljList","");

            model.addAttribute("moduleShowDetailMap", moduleShowDetailMap);

            return "/pc/showed";
        } else {
            logger.info("链接！！！");
            JSONArray jsonArray = JSONUtil.parseArray(moduleShow.getBannerUrl());
            List<String> bannerUrlList = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject myjObject = jsonArray.getJSONObject(i);
                logger.info(myjObject.getString("url"));
                bannerUrlList.add(myjObject.getString("url"));
            }
            moduleShowDetailMap.put("bannerUrlList", bannerUrlList);
            moduleShowDetailMap.put("moduleShowTitle", moduleShow.getTitle());
            moduleShowDetailMap.put("moduleShowEnTitle", moduleShow.getEnTitle());
            moduleShowDetailMap.put("moduleShow", moduleShow);
            moduleShowDetailMap.put("wzList","");
            moduleShowDetailMap.put("ljList",ljList);

            model.addAttribute("moduleShowDetailMap", moduleShowDetailMap);
            return "/pc/showed";
        }


//		if(moduleShowDetailList.size()==1){
//			Map<String,Object> map=new HashMap<>();
//			ModuleShowDetail moduleShowDetail=this.moduleShowDetailService.getById(moduleShowDetailList.get(0).getId());
//			ModuleShow moduleShowTemp=moduleShowService.getById(moduleShowDetail.getModuleShowId());
//			JSONArray jsonArray = JSONUtil.parseArray(moduleShowDetail.getBannerUrl());
//			List<String> bannerUrlList=new ArrayList<>();
//			for(int i=0 ; i < jsonArray.size() ;i++)
//			{
//				JSONObject myjObject = jsonArray.getJSONObject(i);
//				logger.info(myjObject.getString("url"));
//				bannerUrlList.add(myjObject.getString("url"));
//			}
//			map.put("moduleShowDetail",moduleShowDetail);
//			map.put("bannerUrlList",bannerUrlList);
//			map.put("moduleShow",moduleShowTemp);
//			model.addAttribute("map",map);
//			return "/pc/showDetailHuaQingSe";
//		}
//
//		Map<String,Object> map=new HashMap<>();
//		map.put("moduleShowTitle",moduleShow.getTitle());
//		logger.info("BannerUrl:         "+moduleShow.getBannerUrl());
//		JSONArray jsonArray = JSONUtil.parseArray(moduleShow.getBannerUrl());
//		List<String> bannerUrlList=new ArrayList<>();
//		for(int i=0 ; i < jsonArray.size() ;i++)
//		{
//			JSONObject myjObject = jsonArray.getJSONObject(i);
//			logger.info(myjObject.getString("url"));
//			bannerUrlList.add(myjObject.getString("url"));
//		}
//		map.put("bannerUrlList",bannerUrlList);
//		map.put("moduleShowDetailList",moduleShowDetailList);
//		map.put("moduleShowEnTitle",moduleShow.getEnTitle());
//		map.put("moduleShow",moduleShow);
//		logger.info("moduleShowDetailList.size() :                  "   +moduleShowDetailList.size());
//
//		logger.info("getModuleId :   "+moduleShow.getModuleId());
//
//
//		model.addAttribute("map",map);

//		return "/pc/showed";
    }

    @RequestMapping(value = "/showdetailhuaqingse", method = RequestMethod.GET)
    public String showDetailHuaQingSe(@RequestParam(value = "moduleShowDetailId", required = true) Integer moduleShowDetailId, Model model) {
        logger.info("---------welcome--------showDetailHuaQingSe");
        Map<String, Object> map = new HashMap<>();
        ModuleShowDetail moduleShowDetail = this.moduleShowDetailService.getById(moduleShowDetailId);
        ModuleShow moduleShow = moduleShowService.getById(moduleShowDetail.getModuleShowId());
        JSONArray jsonArray = JSONUtil.parseArray(moduleShowDetail.getBannerUrl());
        List<String> bannerUrlList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject myjObject = jsonArray.getJSONObject(i);
            logger.info(myjObject.getString("url"));
            bannerUrlList.add(myjObject.getString("url"));
        }
        map.put("moduleShowDetail", moduleShowDetail);
        map.put("bannerUrlList", bannerUrlList);
        map.put("moduleShow", moduleShow);
        model.addAttribute("map", map);


        return "/pc/showDetailHuaQingSe";
    }

    @RequestMapping(value = "/library", method = RequestMethod.GET)
    public String library(Model model) {
        logger.info("---------welcome--------library");
        List<ModuleShow> imageList = moduleShowService.getListByModuleIdAndType(4, ModuleShow.TYPE_GG);
        Collections.sort(imageList, new Comparator<ModuleShow>() {
            @Override
            public int compare(ModuleShow o1, ModuleShow o2) {
                return o2.getId()-o1.getId();
            }
        });
        List<ModuleShow> moduleShowList = moduleShowService.getListByModuleIdAndType(4, ModuleShow.TYPE_CON);
        ModuleVo moduleVo = new ModuleVo();
        moduleVo.setModuleShowList(moduleShowList);
        moduleVo.setImageList(imageList);
        model.addAttribute("moduleVo", moduleVo);
        logger.info("---------end--------library");
        return "/pc/library";
    }

    @RequestMapping(value = "/activity", method = RequestMethod.GET)
    public String activity(Model model) {
        logger.info("---------begin--------activity");
        List<ModuleShow> imageList = moduleShowService.getListByModuleIdAndType(3, ModuleShow.TYPE_GG);
        Collections.sort(imageList, new Comparator<ModuleShow>() {
            @Override
            public int compare(ModuleShow o1, ModuleShow o2) {
                return o2.getId()-o1.getId();
            }
        });
        List<ModuleShow> moduleShowList = moduleShowService.getListByModuleIdAndType(3, ModuleShow.TYPE_CON);
        ModuleVo moduleVo = new ModuleVo();
        moduleVo.setModuleShowList(moduleShowList);
        moduleVo.setImageList(imageList);
        model.addAttribute("moduleVo", moduleVo);
        logger.info("---------end--------activity");
        return "/pc/activity";
    }

    @RequestMapping(value = "/librarydetailholybible", method = RequestMethod.GET)
    public String libraryDetailHolyBible() {
        logger.info("---------welcome--------showDetailHuaQingSe");
        return "/pc/libraryDetailHolyBible";
    }

    @RequestMapping(value = "/librarydetailfireincairo", method = RequestMethod.GET)
    public String libraryDetailFireInCairo() {
        logger.info("---------welcome--------libraryDetailFireInCairo");
        return "/pc/libraryDetailFireInCairo";
    }

    @RequestMapping(value = "/librarydetailspiritisabone", method = RequestMethod.GET)
    public String libraryDetailSpiritIsABone() {
        logger.info("---------welcome--------libraryDetailSpiritIsABone");
        return "/pc/libraryDetailSpiritIsABone";
    }


    @RequestMapping(value = "/librarydetailinmosttidesanisland", method = RequestMethod.GET)
    public String libraryDetailInMostTidesAnIsland() {
        logger.info("---------welcome--------libraryDetailInMostTidesAnIsland");
        return "/pc/libraryDetailInMostTidesAnIsland";
    }

    @RequestMapping(value = "/librarydetailMISSISSIPPI", method = RequestMethod.GET)
    public String libraryDetailMISSISSIPPI() {
        logger.info("---------welcome--------showDetailHuaQingSe");
        return "/pc/libraryDetailMISSISSIPPI";
    }

    @RequestMapping(value = "/librarydetailrevolution", method = RequestMethod.GET)
    public String libraryDetailRevolution() {
        logger.info("---------welcome--------libraryDetailFireInCairo");
        return "/pc/libraryDetailRevolution";
    }

    @RequestMapping(value = "/activitylecture", method = RequestMethod.GET)
    public String activityLecture() {
        logger.info("---------welcome--------activityLecture");
        return "/pc/activityLecture";
    }


    @RequestMapping(value = "/activitylectureDetailDaFanZhiJian", method = RequestMethod.GET)
    public String activitylectureDetailDaFanZhiJian() {
        logger.info("---------welcome--------activitylectureDetailDaFanZhiJian");
        return "/pc/activitylectureDetailDaFanZhiJian";
    }


    @RequestMapping(value = "/activitylectureDetailJapanPhoto", method = RequestMethod.GET)
    public String activitylectureDetailJapanPhoto() {
        logger.info("---------welcome--------activitylectureDetailJapanPhoto");
        return "/pc/activitylectureDetailJapanPhoto";
    }

    @RequestMapping(value = "/activityforum", method = RequestMethod.GET)
    public String activityForum() {
        logger.info("---------welcome--------activityForum");
        return "/pc/activityForum";
    }

    @RequestMapping(value = "/activityStudio", method = RequestMethod.GET)
    public String activityStudio() {
        logger.info("---------welcome--------activityForum");
        return "/pc/activityStudio";
    }

    @RequestMapping(value = "/educationschool", method = RequestMethod.GET)
    public String educationSchool(@RequestParam(value = "moduleShowId") Integer moduleShowId, Model model) {
        logger.info("---------welcome--------educationSchool");
        List<ModuleShowDetail> moduleShowDetailList = this.moduleShowDetailService.getListByModuleShowIdAndType(moduleShowId, 1);
        model.addAttribute("moduleShowDetailList", moduleShowDetailList);
        return "/pc/educationSchool";
    }


    @RequestMapping(value = "/educationteacher", method = RequestMethod.GET)
    public String educationTeacher() {
        logger.info("---------welcome--------educationTeacher");
        return "/pc/educationTeacher";
    }


    @RequestMapping(value = "/educationworkshop", method = RequestMethod.GET)
    public String educationWorkShop() {
        logger.info("---------welcome--------educationWorkShop");
        return "/pc/educationWorkShop";
    }


    @RequestMapping(value = "/aboutus", method = RequestMethod.GET)
    public String aboutUs(Model model) {
        logger.info("---------welcome--------aboutUs");
        AboutUs aboutUs = aboutUsService.getAll();
        List<AboutUsBanner> bannerList = aboutUsBannerService.getByAboutUsId(aboutUs.getId());
        model.addAttribute("aboutUs", aboutUs);
        model.addAttribute("bannerList", bannerList);
        logger.info("---------end--------aboutUs");


        return "/pc/aboutUs";
    }


    @RequestMapping(value = "/showlightcomesagain", method = RequestMethod.GET)
    public String showLightComesAgain() {
        logger.info("---------welcome--------showLightComesAgain");
        return "/pc/showLightComesAgain";
    }

    //	活动预览
    @RequestMapping(value = "/showReview", method = RequestMethod.GET)
    public String showReview() {
        logger.info("---------welcome--------showLightComesAgain");
        return "/pc/showReview";
    }

    //	摄影工作坊
    @RequestMapping(value = "/educationShotStudio", method = RequestMethod.GET)
    public String educationShotStudio() {
        logger.info("---------welcome--------education");
        return "/pc/educationShotStudio";
    }
}
